#!/usr/bin/env python
# -*- coding:utf-8 -*-

import urllib
import urllib2
import cookielib


def renren_login():
    # 创建一个可以保存cookie对象，CookieJar对象
    cookie_jar = cookielib.CookieJar()

    # 创建Cookie处理器对象
    cookie_handler = urllib2.HTTPCookieProcessor(cookie_jar)

    # 创建自定义对象
    opener = urllib2.build_opener(cookie_handler)

    # 发送请求即可
    headers = {"user-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"}

    #登录参数
    dict_obj = {
        "email": "mr_mao_hacker@163.com",
        "password": "alarmchime"
    }
    data = urllib.urlencode(dict_obj)

    #人人网登录的老接口，只需账号密码即可登录
    login_url = "http://www.renren.com/Plogin.do"
    
    #构建一个post请求，包含了登录的账号密码
    request = urllib2.Request(url = login_url,data = data,headers = headers)

    # urllib2.urlopen()
    # 发送请求，并保存Cookie
    opener.open(request)
    # 返回保存了用户登录状态的opener
    # return opener
    urllib2.install_opener(opener)


def after_login():
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"}

    start_urls = [
        "http://www.renren.com/327550029/profile",
        "http://www.renren.com/410043129/profile",
        "http://www.renren.com/403811175/profile"
    ]

    for i,url in enumerate(start_urls):
        request = urllib2.Request(url,headers = headers)

        response = urllib2.urlopen(request)

        with open(str(i) + ".html","w") as f:
            f.write(response.read())

if __name__ == "__main__":
    renren_login()
    after_login()


